package com.bytz.modules.cms.user.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bytz.common.constant.BooleanConstant;
import com.bytz.common.system.query.MPJLambdaWrapperEx;
import com.bytz.common.system.query.MPJQueryGenerator;
import com.bytz.modules.bpm.client.service.utils.FlowUtils;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.bytz.modules.cms.user.entity.EcAudit;
import com.bytz.modules.cms.user.mapper.EcAuditMapper;
import com.bytz.modules.cms.user.model.EcAuditModel;
import com.bytz.modules.cms.user.service.IEcAuditService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 莱宝CMS—审批管理—EC审批表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class EcAuditServiceImpl extends MPJBaseServiceImpl<EcAuditMapper, EcAudit> implements IEcAuditService {


    @Override
    public IPage<EcAuditModel> queryPage(IPage<EcAuditModel> page, EcAuditModel ecAuditModel, Map<String, String[]> parameterMap) throws Exception {
        MPJLambdaWrapperEx<EcAudit> queryWrapper = MPJQueryGenerator.initQueryWrapper(ecAuditModel, parameterMap, EcAudit.class);

        queryWrapper
                .selectAll(EcAudit.class)
                .select(Order.class, tfi ->
                        !AuditBatchConstants.IGNORE_PROPERTY
                                .contains(
                                        tfi.getProperty()))
                .selectAs(Order::getStatus, "orderStatus")
                .leftJoin(Order.class, on ->
                        on.eq(Order::getId, EcAudit::getOrderId)
                                .eq(Order::getDelFlag, BooleanConstant.INT_FALSE));
        IPage<EcAuditModel> result = baseMapper.selectJoinPage(page, EcAuditModel.class, queryWrapper);
        result.getRecords().forEach(model -> model.setCanAudit(FlowUtils.calculateCanAudit(model)));
        return result;
    }

    @Override
    public EcAuditModel queryById(String id) {
        MPJLambdaWrapper<EcAudit> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(EcAudit.class)
                .select(Order.class, tfi ->
                        !AuditBatchConstants.IGNORE_PROPERTY
                                .contains(
                                        tfi.getProperty()))
                .leftJoin(Order.class, on ->
                        on.eq(Order::getId, EcAudit::getOrderId)
                                .eq(Order::getDelFlag, BooleanConstant.INT_FALSE))
                .eq(EcAudit::getId, id);
        return this.selectJoinOne(EcAuditModel.class, queryWrapper);
    }

    @Override
    public Optional<EcAudit> queryByOrderId(String orderId) {
        Assert.notBlank(orderId, "orderId为空");

        return lambdaQuery().eq(EcAudit::getOrderId, orderId).oneOpt();
    }

    @Override
    public void submitEcAuditFile(String orderId) {
        lambdaUpdate().set(EcAudit::getStatus, OrderConstants.EcStatus.APPROVING)
                .eq(EcAudit::getOrderId, orderId)
                .update();
    }
}